require('web3');
require('ethereumjs-tx');
require('web3-eth-abi');
var express = require('express');
var router = express.Router();
var daoutil = require('./database');
var EtherData = require('./etherdata');
var dao = new daoutil();
var etherData = new EtherData();
var https =  require('https');
//获取nonce值并且加1操作
var nonce = etherData.getNonce();
global.nonce = --nonce;
global.getNextNonce = function () {
    global.nonce++;
    return '0x' + global.nonce.toString(16);
}


router.get('/getOpenId',function(req,res,next){
    // var js_code = req.query.code;
    // var appid = "wx9b1a0a8243ee0526";
    // var secret = "38476eaeb34ec93a75cba999f1408720";
    // var grant_type = "authorization_code";
    // var url = "https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code";
    // https.get(url,function(res){
    //     console.res;
    //     return res;
    // });
});

router.get('/getNoteListById', function (req, res, next) {
    return dao.getNoteListById(req, res);
});
router.get('/getNoteListByIdAndName', function (req, res, next) {
    return dao.getNoteListByIdAndName(req, res);
});

router.get('/newNote', function (req, res, next) {
    var code = etherData.addNote(req.query.id, req.query.name, req.query.content);
    console.log("*********addNotecode:" + code + "********");
    req.query['code'] = code;
    return dao.addNote(req, res);
});

router.get('/editNote', function (req, res, next) {
    var code = etherData.updateNote(req.query.id, req.query.name, req.query.content);
    console.log("*********updateNotecode:" + code + "********");

    req.query['code'] = code;
    return dao.updateNote(req, res);
});

router.get('/getContent', function (req, res, next) {
    var user_id = req.query.id;
    var name = req.query.name;
    res.json(etherData.getNote(user_id, name))
});

router.get('/deleteNote',function(req,res,next){
    var user_id = req.query.id;
    var name = req.query.name;
    etherData.deleteNote(user_id, name);
    return dao.deleteNote(req,res);
})

router.get('/getStatus',function(req,res,next){
    res.json(etherData.queryTransactionStatus(req.query.hash));
})

module.exports = router;

